package com.shajj.data.record;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.shajj.data.Record;
import com.shajj.data.ShajjUtil;

import weibo4j.Weibo;

@SuppressWarnings("serial")
public class ShajjcdnServlet extends HttpServlet {
	private static final Logger log = Logger.getLogger("shajj");

	private static final String target = "http://www.caoegg.cn/?page=1";

	private static final String USERID = "echozhjun@sina.com";

	private static final String PASSWORD = "123456";

	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		resp.setContentType("text/plain;charset=utf-8");
		try {
			Pattern pattern = Pattern.compile("<script>share\\((\\d+), \"(.*?)\"\\);<\\/script>", Pattern.DOTALL);
			Matcher matcher = pattern.matcher(getContent());
			while (matcher.find()) {
				if (matcher.groupCount() > 1) {
					try {
						String text = matcher.group(2);
						Long id = Long.parseLong(matcher.group(1));
						Record record = RecordDataManager.getInstance().get(id);
						if (record == null) {
							RecordDataManager.getInstance().save(new Record(id, text));
							log.warning("data svaed:" + id);
							post(getWeiboContent(text));
						}
					} catch (Exception e) {
						continue;
					}
				}
			}
		} catch (Exception e) {
			log.warning("抓取数据错误：" + e.getMessage());
		}

	}

	private String getWeiboContent(String source) {
		return ShajjUtil.replace(source) + " 更多精彩：傻鸡鸡网http://shajj.com";
	}

	private String getContent() {
		try {
			URL url = new URL(target);
			BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8"));
			String line;
			StringBuilder sb = new StringBuilder();
			while ((line = reader.readLine()) != null) {
				sb.append(line);
			}
			reader.close();
			return sb.toString();
		} catch (Exception e) {
			log.warning("获取页面信息错误" + e.getMessage());
		}

		return "";

	}

	private void post(String content) {
		System.setProperty("weibo4j.oauth.consumerKey", Weibo.CONSUMER_KEY);
		System.setProperty("weibo4j.oauth.consumerSecret", Weibo.CONSUMER_SECRET);
		try {
			Weibo weibo = getWeibo(USERID, PASSWORD);
			weibo.updateStatus(content.length() > 130 ? content.substring(0, 130) + "……": content);
			log.warning("post message:" + (content.length() > 20 ? content.substring(0, 20) :  content));
		} catch (Exception e) {
			log.warning("围脖发送失败" + e.getMessage());
		}
	}

	private Weibo getWeibo(String userID, String password) {
		Weibo weibo = new Weibo();
		weibo.setUserId(userID);
		weibo.setPassword(password);
		return weibo;
	}


	public static void main(String[] args) {
		String s = "一二";
		System.out.println(s.substring(0,1));
	}
}
